package org.chen.scene.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.result.ResultVO;
import org.chen.common.scene.task.GrabTask;
import org.chen.common.scene.task.GrabTaskLog;
import org.chen.scene.service.task.GrabTaskLogService;
import org.chen.scene.service.task.impl.GrabTaskServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Tag(name = "不重要的接口", description = "不重要的接口")
@RequestMapping("/grab")
public class GrabTaskController {
    @Autowired
    private GrabTaskServiceImpl grabTaskService;

    /**
     * 查询任务列表
     */
    @GetMapping("/list")
    public ResultVO getAllTask() {
        try {
            List<GrabTask> grabTaskList = grabTaskService.getAllTask();
            return ResultVO.success(grabTaskList);
        } catch (Exception e) {
            log.error("查询任务列表异常", e);
            return ResultVO.fail("查询失败：" + e.getMessage());
        }
    }

    @PostMapping("/create")
    public ResultVO createTask(@RequestBody GrabTask task) {
        try {
            grabTaskService.addGrabTask(task);
            return ResultVO.success("任务创建成功");
        } catch (Exception e) {
            return ResultVO.fail("任务创建失败: " + e.getMessage());
        }
    }

    @PutMapping("/update/{id}")
    public ResultVO updateTask(@PathVariable Long id, @RequestBody GrabTask task) {
        try {
            task.setId(id);
            GrabTask grabTask = grabTaskService.updateGrabTask(task);
            return ResultVO.success("任务更新成功", grabTask);
        } catch (Exception e) {
            return ResultVO.fail("任务更新失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResultVO deleteTask(@PathVariable Long id) {
        try {
            grabTaskService.deleteGrabTask(id);
            return ResultVO.success("任务删除成功");
        } catch (Exception e) {
            return ResultVO.fail("任务删除失败: " + e.getMessage());
        }
    }

    @PostMapping("/{id}/execute")
    public ResultVO executeTaskNow(@PathVariable Long id) {
        try {
            grabTaskService.executeScheduledTaskNow(id);
            return ResultVO.success("任务执行已启动");
        } catch (Exception e) {
            return ResultVO.fail("执行任务失败: " + e.getMessage());
        }
    }
}
